<!DOCTYPE HTML>
<html>
  <head>
	<?php echo Yaf_View_Simple::render("header.phtml"); ?>
  </head>
  <body>
    <?php echo Yaf_View_Simple::render("menu.phtml"); ?>
    <div class="container">
	  <div class="content"><table class="table table-condensed">
        <thead>
	      <tr>
            <th>ID</th>
	        <th>Title</th>
	        <th>Link</th>
	        <th>Status</th>
	        <th>Operation</th>
	      </tr>
        </thead>
        <tbody id="tbody">
          <?php function detree($arg,$level = 0){ ?>
          <?php $px = ($level*20).'px'; ?>
          <?php $level = $level+1; ?>
          <?php foreach($arg as $value){ ?>
          <tr>
            <td><?php echo $value['id'] ?></td>
            <td><p style="padding-left:<?php echo $px; ?>"><?php echo $value['title']; ?></p></td>
            <td><?php echo $value['link']; ?></td>
            <td>
              <?php if($value['status'] ==1){ ?>
              <button type="button" class="btn btn-default" data-status="<?php echo $value['status'] ?>" data-id="<?php echo $value['id']; ?>" onclick = "page.status('/admin/category/status/',this)">
                <span class="glyphicon glyphicon-ok" aria-hidden="true">
                </span>
              </button>
              <?php }else{ ?>
              <button type="button" class="btn btn-default" data-status="<?php echo $value['status'] ?>" data-id="<?php echo $value['id']; ?>" onclick = "page.status('/admin/category/status/',this)">
                <span class="glyphicon glyphicon-off" aria-hidden="true">
                </span>
              </button>
              <?php } ; ?>
            </td>
            <td>
              <div class="btn-group" role="group" aria-label="...">
                <a type="button" href="./edit?id=<?php echo $value['id']; ?>" class="btn btn-default">
                  <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
                </a>

                <button type="button-remove" class="btn btn-default" onclick ="page.remove(this)" data-value="<?php echo $value['id']; ?>">
                  <span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
                </button>
              </div>  
            </td>
          </tr>
          <?php if(isset($value['_'])){ ?>
          <?php detree($value['_'],$level); ?>
          <?php } ?>
          
          <?php } } ?>
          <?php detree($nav,0); ?>
        </tbody>
      </table>

      </div>
    </div>
    <!-- Modal -->
    <div class="modal" id="imodal" tabindex="-1" role="dialog" aria-labelledby="modallable">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-body bg-danger">
            <p>Are you sure to remove?</p>
          </div>
          <div class="modal-footer">
            <button type="button" id="close" class="btn btn-primary">Yes</button>
          </div>          
        </div>
      </div>
    </div>        
    
	<?php echo Yaf_View_Simple::render("footer.phtml"); ?>
    <script type="text/javascript">
     ;(function(){
         var status = function(url,obj){
	         that = $(obj);
	         _this = this;
             var data = {
                 'id' :that.attr('data-id'),
                 'status':parseInt(that.attr('data-status')),
             }
             $.ajax({
                 type:"post",
                 url :url,
                 data:data,
                 success:function(e){
                     if(e.status == 1){
                         if(data.status == 1){
                             var span = that.find('span');
                             span.removeClass();
                             span.addClass('glyphicon glyphicon-off');
                             that.attr({'data-status':0})
                         }else{
                             var span = that.find('span');
                             span.removeClass();
                             span.addClass('glyphicon glyphicon-ok')
                             that.attr({'data-status':1})
                         }
                     }else{
                         return false;
                     }
                 },
                 dataType:'json'
             });
         }
         var remove = function(obj){
             that = $(obj);
             var modal = $('#imodal');
             modal.modal();
             
             modal.find('#close').on('click',function(){
                 var id = that.attr('data-value');
                 var data = {'id':id};
                 var url = '/admin/navigation/remove';
                 
                 $.ajax({
                     type:"post",
                     url : url,
                     data: data,
                     success:function(e){
                         if(e.status == 1){
                             that.parent().parent().parent().remove();
                             modal.modal('hide');
                         }else{
                             return false;
                         }
                     },
                     dataType: 'json',
                 });
             })
             
         }
         return page = {
             status : status,
             remove : remove
         }         
     }());         
    </script>    
  </body>
</html>
